"""
Case Type   : 共享存储-工具-pg_controldata
Case Name   : 主要功能验证
Create At   : 2023/4/11
Owner       : opentestcase019
Description :
    1.显示数据库目录下control文件的信息（所有）
    2.显示数据库目录下control文件的信息（节点0）
    3.显示数据库目录下control文件的信息（节点1）
Expect      :
    1.显示正确
    2.显示正确
    3.显示正确
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()

    def test_sharestorage_tool(self):
        step1 = "-----step1:显示数据库目录下control文件的信息（所有）;expect:显示正确-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_controldata --enable-dss " \
            f"--socketpath={macro.UDS_PATH} +data"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(Primary_SH.get_node_num(),
                          execute_msg.count("pg_control data (instance id"),
                          "执行失败" + step1)

        step2 = "-----step2.显示数据库目录下control文件的信息（节点0）;expect:显示正确-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_controldata --enable-dss " \
            f"--socketpath={macro.UDS_PATH} +data -I 0"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("pg_control data (instance id 0)",
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3.显示数据库目录下control文件的信息（节点1）;expect:显示正确-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_controldata --enable-dss " \
            f"--socketpath={macro.UDS_PATH} +data -I 1"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("pg_control data (instance id 1)",
                      execute_msg, "执行失败" + step3)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
